/*
 * @lc app=leetcode.cn id=653 lang=cpp
 *
 * [653] 两数之和 IV - 输入 BST
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    void inorder(TreeNode* root,vector<int>& res)
    {
        if(root)
        {
            inorder(root->left,res);
            res.push_back(root->val);
            inorder(root->right,res);
        }
    }

    bool findTarget(TreeNode* root, int k) {
        vector<int> v_BST;
        inorder(root,v_BST);

        int left = 0; 
        int right = v_BST.size() - 1;

        while(left < right)
        {
            int res = v_BST[left] + v_BST[right];
            if(res == k)
                return true;
            else if(res < k )
            {
                left++;
            }
            else if( res > k)
            {
                right--;
            }
        }
        return false;
    }
};
// @lc code=end

